"use strict";

/*
 * MikoPBX - free phone system for small business
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

/* global globalDebugMode, EventSource */

/**
 * The connectionCheckWorker object is responsible for checking
 * the connection status with backend
 *
 * @module connectionCheckWorker
 */
var connectionCheckWorker = {
  // Counter for error counts
  errorCounts: 0,

  /**
   * jQuery object for the no connection dimmer element.
   * @type {jQuery}
   */
  $connectionDimmer: $('#connection-dimmer'),

  /**
   * EventSource object for the connection check.
   * @type {EventSource}
   */
  eventSource: null,

  /**
   * Initialize the connection check worker.
   */
  initialize: function initialize() {
    connectionCheckWorker.eventSource = new EventSource('/connection-check');
    connectionCheckWorker.eventSource.addEventListener('error', function (event) {
      connectionCheckWorker.cbAfterResponse(false);
    });
    connectionCheckWorker.eventSource.addEventListener('open', function (event) {
      connectionCheckWorker.cbAfterResponse(true);
    });
  },

  /**
   * Callback function after receiving the response from the connection check.
   * @param {boolean} result - The result of the connection check.
   */
  cbAfterResponse: function cbAfterResponse(result) {
    if (result === true) {
      // If the connection is successful, hide the connection dimmer
      connectionCheckWorker.$connectionDimmer.dimmer('hide'); // Reload the page if the error count exceeds a certain threshold

      if (connectionCheckWorker.errorCounts > 5) {
        window.location.reload();
      } // Reset the error count


      connectionCheckWorker.errorCounts = 0;
    } else if (connectionCheckWorker.errorCounts > 3) {
      // If the connection is unsuccessful and error count exceeds a threshold, show the connection dimmer
      connectionCheckWorker.$connectionDimmer.dimmer('show'); // Increment the error count

      connectionCheckWorker.errorCounts += 1;
    } else {
      // Increment the error count
      connectionCheckWorker.errorCounts += 1;
    }
  }
}; // When the document is ready, initialize the connection check worker

$(document).ready(function () {
  if (!globalDebugMode) {
    connectionCheckWorker.initialize();
  }
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9tYWluL2Nvbm5lY3Rpb24tY2hlY2std29ya2VyLmpzIl0sIm5hbWVzIjpbImNvbm5lY3Rpb25DaGVja1dvcmtlciIsImVycm9yQ291bnRzIiwiJGNvbm5lY3Rpb25EaW1tZXIiLCIkIiwiZXZlbnRTb3VyY2UiLCJpbml0aWFsaXplIiwiRXZlbnRTb3VyY2UiLCJhZGRFdmVudExpc3RlbmVyIiwiZXZlbnQiLCJjYkFmdGVyUmVzcG9uc2UiLCJyZXN1bHQiLCJkaW1tZXIiLCJ3aW5kb3ciLCJsb2NhdGlvbiIsInJlbG9hZCIsImRvY3VtZW50IiwicmVhZHkiLCJnbG9iYWxEZWJ1Z01vZGUiXSwibWFwcGluZ3MiOiI7O0FBQUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTs7QUFFQTs7QUFFQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQSxJQUFNQSxxQkFBcUIsR0FBRztBQUUxQjtBQUNBQyxFQUFBQSxXQUFXLEVBQUUsQ0FIYTs7QUFLMUI7QUFDSjtBQUNBO0FBQ0E7QUFDSUMsRUFBQUEsaUJBQWlCLEVBQUVDLENBQUMsQ0FBQyxvQkFBRCxDQVRNOztBQVcxQjtBQUNKO0FBQ0E7QUFDQTtBQUNJQyxFQUFBQSxXQUFXLEVBQUUsSUFmYTs7QUFpQjFCO0FBQ0o7QUFDQTtBQUNJQyxFQUFBQSxVQXBCMEIsd0JBb0JiO0FBQ1RMLElBQUFBLHFCQUFx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